﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class product_insurance_admin_applicant_details_hospital : System.Web.UI.Page
{

    ApplicantDetailsHospitalBLL bll = new ApplicantDetailsHospitalBLL(); 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        int id = int.Parse(Request.QueryString["applicationID"]);
        bll.applicationID = id;
        this.applicationID.Value = Convert.ToString(id); 

        DataSet self = bll.getSelfDetail();
        this.Self_FormView.DataSource = bll.getSelfDetail();
        this.Self_FormView.DataBind();

        String shortUrl = bll.getApplicationDoc();

        if (Util.IsBlank(shortUrl))
        {
            this.link_view.Visible = false;
            this.lb_link_view.Visible = false;
        }
        else
        {
            this.lb_link_view.Visible = true;
            this.link_view.Visible = true;
            this.link_view.NavigateUrl=shortUrl;
        }

        string custId = self.Tables[0].Rows[0]["custID"].ToString();
        this.custId.Value = custId;


        this.Relation_Repeater.DataSource = bll.getFamillyDetail();
        this.Relation_Repeater.DataBind();


        Employee_FormView.DataSource = bll.getSelfDetail();
        Employee_FormView.DataBind();

        this.IllNess_Repeater.DataSource=bll.getIllnessList();
        this.IllNess_Repeater.DataBind();

        this.Foreseeable_Repeater.DataSource=bll.getForeseeableList();
        this.Foreseeable_Repeater.DataBind();

        this.DeclinedHistory_Repeater.DataSource = bll.getDeclinedHistory();
        this.DeclinedHistory_Repeater.DataBind();

        this.Hyperlipidaemia_Repeater.DataSource = bll.getHyperlipidaemiaList();
        this.Hyperlipidaemia_Repeater.DataBind();

        this.Plan_Repeater.DataSource = bll.getPlanList();
        this.Plan_Repeater.DataBind();

        DateTime startDate = (DateTime) self.Tables[0].Rows[0]["startDate"];
        DateTime endDate = (DateTime)self.Tables[0].Rows[0]["endDate"];

        this.tbStartDate.Text = startDate.ToString("dd/MM/yyyy");
        this.tbEndDate.Text = endDate.ToString("dd/MM/yyyy");

        string paymentPeriod = self.Tables[0].Rows[0]["paymentPeriod"].ToString();
        this.lb_paymentPeriod.Text = paymentPeriod;


       

        }

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
      
        LinkedList<int> levels = new LinkedList<int>();
        LinkedList<int> insuredIds = new LinkedList<int>();

        int count = this.Plan_Repeater.Items.Count;
        for (int i = 0; i < count; i++)
        {
            CheckBox cb_accept = (CheckBox)this.Plan_Repeater.Items[i].FindControl("cb_accept");
            DropDownList ddl = (DropDownList)this.Plan_Repeater.Items[i].FindControl("ddlPremiumLevel");
            HiddenField hf_insuredId = (HiddenField)this.Plan_Repeater.Items[i].FindControl("hf_insuredId");

            if (cb_accept.Checked)
            {
                int selectValue = int.Parse(ddl.SelectedValue);
                levels.AddLast(selectValue);
                insuredIds.AddLast(int.Parse (hf_insuredId.Value));
            }

        }

          var approval = new ApprovalBLL
        {
            CustID =this.custId.Value,
            StaffID = (String)Session["userId"],
            ApplicationInsuranceId =int.Parse(this.applicationID.Value),
            Type = "INSURANCE",
            Category = "H",
            StartDate = Convert.ToDateTime(this.tbStartDate.Text),
            EndDate = Convert.ToDateTime(this.tbEndDate.Text)
        };

          approval.levels = levels;
          approval.applyInsuredIds = insuredIds;

        approval.approved();

        this.Response.Redirect("./insurance_applicant_list.aspx");

      /*   String insuredIdsParamter = getUrlParamter(insuredIds);
        String levelsParamter = getUrlParamter(levels);

     

        this.Response.Redirect("./accept_application.aspx?custId=" + this.custId.Value
            + "&" + "applicationID=" + this.applicationID.Value + "&" + "insuredId=" + insuredIdsParamter + "&" + "premiumLevel=" + levelsParamter

            
            
            );
 
            */

    }


    private String getUrlParamter(LinkedList<int> values)
    {
        if (values == null) return "";
        StringBuilder sb = new StringBuilder();
        foreach (int obj in values)
        {
            
            sb.Append(obj);
            sb.Append(",");

        }

        String valueString = sb.ToString().TrimEnd(',');

        return valueString;
    }
}